<?php

namespace app\api\logic;

use app\common\exception\BusinessException;

class Feedback
{
    /**
     * 添加
     * @param array $params
     * @throws
     */
    public function add(array $params)
    {
        //组装数据
        $time = time();
        $data = [
            'type' => $params['type'],
            'user_id' => $params['userId'],
            'detail' => $params['detail'],
            'contact' => $params['contact'] ?? '',
            'a_time' => $time,
            'u_time' => $time,
        ];

        //新增数据
        $isInsert = app(\app\common\model\Feedback::class)->insert($data);
        if (!$isInsert)
        {
            throw new BusinessException('新增反馈信息失败');
        }
    }

    /**
     * 列表
     * @param array $param
     * @return array
     */
    public function getList(array $param): array
    {
        // 查询条件
        $where = [
            'user_id|to_user_id' => $param['userId']
        ];

        // 查询列表
        $list = app(\app\common\model\Feedback::class)->where($where)->order('id', 'desc')->limit(3)->select();
        $list = $list ? $list->toArray() : [];

        // 处理数据
        foreach ($list as $key => $val)
        {
            $list[$key]['a_time'] = date('Y-m-d H:i:s', $val['a_time']);
        }

        // 返回
        return $list;
    }
}